subroutine apj_fortran_transform_oei( &
       ni, & ! (in)    range of i
       na, & ! (in)    range of a
      Jsi, & ! (in)    single index jastrow intermediate for i
      Jsa, & ! (in)    single index jastrow intermediate for a
     Jdia, & ! (in)    double index jastrow intermediate for i and a
      Tia)   ! (inout) one electron integrals

implicit none

integer,      intent(in)    ::     ni
integer,      intent(in)    ::     na
real(kind=8), intent(in)    ::    Jsi(ni)
real(kind=8), intent(in)    ::    Jsa(na)
real(kind=8), intent(in)    ::   Jdia(ni,na)
real(kind=8), intent(inout) ::    Tia(ni,na)

integer :: i, a

do a = 1,na
do i = 1,ni
  Tia(i,a) = Tia(i,a) * Jsi(i) * Jsa(a) * Jdia(i,a)
enddo
enddo

end subroutine apj_fortran_transform_oei




subroutine apj_fortran_transform_tei( &
       ni, & ! (in)    range of i
       na, & ! (in)    range of a
       nj, & ! (in)    range of j
       nb, & ! (in)    range of b
      Jsi, & ! (in)    single index jastrow intermediate for i
      Jsa, & ! (in)    single index jastrow intermediate for a
      Jsj, & ! (in)    single index jastrow intermediate for j
      Jsb, & ! (in)    single index jastrow intermediate for b
     Jdia, & ! (in)    double index jastrow intermediate for i and a
     Jdij, & ! (in)    double index jastrow intermediate for i and j
     Jdib, & ! (in)    double index jastrow intermediate for i and b
     Jdaj, & ! (in)    double index jastrow intermediate for a and j
     Jdab, & ! (in)    double index jastrow intermediate for a and b
     Jdjb, & ! (in)    double index jastrow intermediate for j and b
    Tiajb)   ! (inout) two electron integrals in 1122 order

implicit none

integer,      intent(in)    ::     ni
integer,      intent(in)    ::     na
integer,      intent(in)    ::     nj
integer,      intent(in)    ::     nb
real(kind=8), intent(in)    ::    Jsi(ni)
real(kind=8), intent(in)    ::    Jsa(na)
real(kind=8), intent(in)    ::    Jsj(nj)
real(kind=8), intent(in)    ::    Jsb(nb)
real(kind=8), intent(in)    ::    Jdia(ni,na)
real(kind=8), intent(in)    ::    Jdij(ni,nj)
real(kind=8), intent(in)    ::    Jdib(ni,nb)
real(kind=8), intent(in)    ::    Jdaj(na,nj)
real(kind=8), intent(in)    ::    Jdab(na,nb)
real(kind=8), intent(in)    ::    Jdjb(nj,nb)
real(kind=8), intent(inout) ::   Tiajb(ni,na,nj,nb)

integer :: i, a, j, b

real(kind=8) :: xi, xa, xj, xb

do b = 1,nb
  xb = Jsb(b)
  do j = 1,nj
    xj = xb * Jsj(j) * Jdjb(j,b)
    do a = 1,na
      xa = xj * Jsa(a) * Jdaj(a,j) * Jdab(a,b)
      do i = 1,ni
        xi = xa * Jsi(i) * Jdia(i,a) * Jdij(i,j) * Jdib(i,b)
        Tiajb(i,a,j,b) = Tiajb(i,a,j,b) * xi
      enddo
    enddo
  enddo
enddo

end subroutine apj_fortran_transform_tei




!subroutine apj_fortran_der_transform_final_mult(n, Z, T)
!
!implicit none
!
!integer,      intent(in)    ::     n
!real(kind=8), intent(in)    ::     Z(n)
!real(kind=8), intent(inout) ::     T(n)
!
!integer :: i
!
!do i=1,n
!  T(i) = T(i) * Z(i)
!enddo
!
!end subroutine apj_fortran_der_transform_final_mult(n, T, Z)




subroutine apj_fortran_der_trans_oei( &
       ni, & ! (in)    range of i
       na, & ! (in)    range of a
      Jne, & ! (in)    non-excitation jastrow intermediate
      Jsi, & ! (in)    single index jastrow intermediate for i
      Jsa, & ! (in)    single index jastrow intermediate for a
     Jdia, & ! (in)    double index jastrow intermediate for i and a
      Tia)   ! (inout) one electron integrals

implicit none

integer,      intent(in)    ::     ni
integer,      intent(in)    ::     na
real(kind=8), intent(in)    ::    Jne
real(kind=8), intent(in)    ::    Jsi(ni)
real(kind=8), intent(in)    ::    Jsa(na)
real(kind=8), intent(in)    ::   Jdia(ni,na)
real(kind=8), intent(inout) ::    Tia(ni,na)

integer :: i, a

real(kind=8) :: x

do a = 1,na
do i = 1,ni
  x = Jne + Jsi(i) + Jsa(a) + Jdia(i,a)
  Tia(i,a) = Tia(i,a) * x
enddo
enddo

end subroutine apj_fortran_der_trans_oei




subroutine apj_fortran_der_trans_tei( &
       ni, & ! (in)    range of i
       na, & ! (in)    range of a
       nj, & ! (in)    range of j
       nb, & ! (in)    range of b
      Jne, & ! (in)    non-excitation jastrow intermediate
      Jsi, & ! (in)    single index jastrow intermediate for i
      Jsa, & ! (in)    single index jastrow intermediate for a
      Jsj, & ! (in)    single index jastrow intermediate for j
      Jsb, & ! (in)    single index jastrow intermediate for b
     Jdia, & ! (in)    double index jastrow intermediate for i and a
     Jdij, & ! (in)    double index jastrow intermediate for i and j
     Jdib, & ! (in)    double index jastrow intermediate for i and b
     Jdaj, & ! (in)    double index jastrow intermediate for a and j
     Jdab, & ! (in)    double index jastrow intermediate for a and b
     Jdjb, & ! (in)    double index jastrow intermediate for j and b
    Tiajb)   ! (inout) two electron integrals in 1122 order

implicit none

integer,      intent(in)    ::     ni
integer,      intent(in)    ::     na
integer,      intent(in)    ::     nj
integer,      intent(in)    ::     nb
real(kind=8), intent(in)    ::    Jne
real(kind=8), intent(in)    ::    Jsi(ni)
real(kind=8), intent(in)    ::    Jsa(na)
real(kind=8), intent(in)    ::    Jsj(nj)
real(kind=8), intent(in)    ::    Jsb(nb)
real(kind=8), intent(in)    ::    Jdia(ni,na)
real(kind=8), intent(in)    ::    Jdij(ni,nj)
real(kind=8), intent(in)    ::    Jdib(ni,nb)
real(kind=8), intent(in)    ::    Jdaj(na,nj)
real(kind=8), intent(in)    ::    Jdab(na,nb)
real(kind=8), intent(in)    ::    Jdjb(nj,nb)
real(kind=8), intent(inout) ::   Tiajb(ni,na,nj,nb)

integer :: i, a, j, b

real(kind=8) :: xi, xa, xj, xb

do b = 1,nb
  xb = Jsb(b)
  do j = 1,nj
    xj = xb + Jsj(j) + Jdjb(j,b)
    do a = 1,na
      xa = xj + Jsa(a) + Jdaj(a,j) + Jdab(a,b)
      do i = 1,ni
        xi = xa + Jne + Jsi(i) + Jdia(i,a) + Jdij(i,j) + Jdib(i,b)
        Tiajb(i,a,j,b) = Tiajb(i,a,j,b) * xi
      enddo
    enddo
  enddo
enddo

end subroutine apj_fortran_der_trans_tei
